Page({
  data: {
    cartCount: '',
    allowSellAmount: '',
    expemotFreight: "",
    totalMoner: "",
    cartList: [],
    failuregoodsList: [],
  },
  onLoad: function (options) {

  },
  onShow: function () {
    getApp().getCartNum(this);
    this.getCartList();
    this.getCartNum();
  },
  bottomNav(e) {
    getApp(e).bottomNav(e);
  },
  //获取购物车数量
  getCartNum() {
    var url = getApp().data.url + 'shoppingcart/getCartCount';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      is_check: '1'
    };
    getApp().wxAjax(this.returnCartNum, url, body);
  },
  returnCartNum(res) {
    this.setData({
      cartCount: res.data.body.num
    })
    this.judeAllselect();
  },
  //获得购物车数据
  getCartList() {
    var url = getApp().data.url + 'shoppingcart/getMyShoppingCarts';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id')
    };
    wx.showLoading({
      title: '',
      icon: 'loading'
    })
    getApp().wxAjax(this.splitCartList, url, body);
  },
  splitCartList(res) {
    wx.hideLoading();
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      console.log(resObj.body);
      //普通商品
      var nomalCartList = [];
      var allowSellAmount = 0;
      var expemotFreight = 0;
      if (resObj.body.cart_product_type_list[0] != undefined) {
        nomalCartList = resObj.body.cart_product_type_list[0].cart_info_product_list[0].cart_product_list;
        allowSellAmount = resObj.body.allow_sell_amount;
        expemotFreight = resObj.body.exempt_freight;
      }
      //拼团商品
      var ptgoodsCartList = [];
      if (resObj.body.goods_coupon_item_list[0] != undefined) {
        ptgoodsCartList = resObj.body.goods_coupon_item_list;
      }
      //失效商品
      var failuregoodsList = [];
      if (resObj.body.lose_efficacy_items != undefined) {
        failuregoodsList = resObj.body.lose_efficacy_items;
      }
      console.log(ptgoodsCartList)
      this.setData({
        allowSellAmount: allowSellAmount,
        expemotFreight: expemotFreight,
        totalMoner: resObj.body.total_money,
        normalCartList: nomalCartList,
        ptgoodsCartList: ptgoodsCartList,
        failuregoodsList: failuregoodsList,
        cartNum: resObj.body.cart_count
      })
      this.getCartNum();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //增加商品
  addNum(e) {
    var skuid = e.currentTarget.dataset.skuid;
    var check = e.currentTarget.dataset.check;
    var quantity = e.currentTarget.dataset.quantity;
    var body = {};
    body.hander_type = 1;
    body.is_check = check;
    body.supermarket_id = wx.getStorageSync('supermarket_id');
    body.quantity = 1;
    body.sku = skuid;
    console.log(body)
    var url = getApp().data.url + 'shoppingcart/singleSynShoppingCart';
    getApp().wxAjax(this.addNunSug, url, body);
  },
  addNunSug(res) {
    var resObj = res.data;
    console.log(resObj)
    if (resObj.header.res_code == 0) {
      this.getCartList();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  // 减少商品
  cutNum(e) {
    var skuid = e.currentTarget.dataset.skuid;
    var check = e.currentTarget.dataset.check;
    var quantity = e.currentTarget.dataset.quantity;
    var body = {};
    body.hander_type = 1;
    body.is_check = check;
    body.supermarket_id = wx.getStorageSync('supermarket_id');
    body.quantity = -1;
    body.sku = skuid;
    var url = getApp().data.url + 'shoppingcart/singleSynShoppingCart';
    if (quantity == 1) {
      this.deleGoods(e, body);
    } else {
      getApp().wxAjax(this.cutNumSug, url, body);
    }

  },
  cutNumSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.getCartList();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  // 删除商品
  deleGoods(e, body) {
    console.log(body)
    if (typeof body == "undefined") {
      var body = {};
      var skuid = e.currentTarget.dataset.skuid;
      var check = e.currentTarget.dataset.check;
      var quantity = e.currentTarget.dataset.quantity;
      body.hander_type = 1;
      body.is_check = check;
      body.supermarket_id = wx.getStorageSync('supermarket_id');
      body.quantity = -1;
      body.sku = skuid;
    }
    console.log(body)
    var me = this;
    wx.showModal({
      content: '确定删除该商品？',
      cancelText: '再想想',
      cancelColor: '#999999',
      success: function (res) {
        if (res.confirm) {
          me.toDeleGoods(body);
        } else if (res.cancel) {

        }
      }
    })
  },
  toDeleGoods(body) {
    body.hander_type = 2;
    var url = getApp().data.url + 'shoppingcart/singleSynShoppingCart';
    getApp().wxAjax(this.deleSug, url, body);
  },
  deleSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.getCartList();
    } else {
      console.log(res)
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //选中商品
  selectGoods(e) {
    var skuid = e.currentTarget.dataset.skuid;
    var kind = e.currentTarget.dataset.kind;
    console.log(kind)
    var body = {};
    body.hander_type = 1;
    if (kind == "select") {
      body.is_check = 2;
    } else {
      body.is_check = 1;
    }
    body.supermarket_id = wx.getStorageSync('supermarket_id');
    body.quantity = 0;
    body.sku = skuid;
    var url = getApp().data.url + 'shoppingcart/singleSynShoppingCart';
    getApp().wxAjax(this.selsctSug, url, body);
  },
  selsctSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.getCartList();
    } else {
      console.log(res)
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //全选全不选
  judeAllselect() {
    var allNum = this.data.cartNum;
    var checkNum = this.data.cartCount;
    if (checkNum == allNum) {
      this.setData({
        allSelectFlage: 0
      })
    } else {
      this.setData({
        allSelectFlage: 1
      })
    }
  },
  allSelect(e) {
    var kind = e.currentTarget.dataset.kind;
    var body = {};
    body.is_uncheck = 0;
    body.supermarket_id = wx.getStorageSync('supermarket_id');
    var handerCart = [];
    var nmorList = this.data.normalCartList;
    var ptspList = this.data.ptgoodsCartList;
    for (let i = 0; i < nmorList.length; i++) {
      var obj = {};
      obj.hander_type = 1;
      if (kind == "select") {
        obj.is_check = 0;
      } else {
        obj.is_check = 1;
      }

      obj.quantity = 0;
      obj.sku = nmorList[i].sku;
      handerCart.push(obj)
    }
    for (let i = 0; i < ptspList.length; i++) {
      var obj = {};
      obj.hander_type = 1;
      if (kind == "select") {
        obj.is_check = 0;
      } else {
        obj.is_check = 1;
      }
      obj.quantity = 0;
      obj.sku = ptspList[i].sku;
      handerCart.push(obj)
    }
    body.hander_cart = handerCart;
    var url = getApp().data.url + 'shoppingcart/synAllShoppingCart';
    getApp().wxAjax(this.allSelectSug, url, body);
  },
  allSelectSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      this.getCartList();
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //提交订单
  setOrder() {
    var body = {};
    body.address_id = wx.getStorageSync('addressId');
    body.supermarket_id = wx.getStorageSync('supermarket_id');
    body.use_coupon = 1;
    if (this.data.cartCount == 0) {
      wx.showToast({
        title: '没有选择商品哦',
        icon: 'none'
      });
      return false;
    }
    var url = getApp().data.url + 'order/settlementOrderInfo';
    getApp().wxAjax(this.submitOriderSug, url, body);
  },
  submitOriderSug(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0 || resObj.header.res_code == 6001008 || resObj.header.res_code == 6001006) {
      console.log(resObj)
      wx.navigateTo({
        url: '../confirmOrder/confirmOrder',
      })
      wx.setStorageSync('orderObj', resObj.body)
    } else {
      console.log(resObj)
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  gotosee() {
    wx.redirectTo({
      url: '../eIndex/eIndex',
    })
  }
})